package org.seckill.custommapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.Map;

/**
 * @author Wrb
 * @date 2019/7/3 14:56
 */
public interface SeckillCustomMapper {

	/**
	 * 减库存
	 *
	 * @param seckillId
	 * @param killTime
	 * @return
	 */
	@Update({
			"UPDATE seckill SET number = number - 1 ",
			"WHERE seckill_id = #{seckillId} and start_time <= #{killTime}",
			"and end_time >= #{killTime} and number > 0"
	})
	int reduceNumber(@Param("seckillId") Long seckillId, @Param("killTime") Date killTime);

	//mybatis 注解 调用存储过程
	@Select({
			"call execute_seckill(",
			"#{map.seckillId,mode=IN,jdbcType=BIGINT},",
			"#{map.userPhone,mode=IN,jdbcType=BIGINT},",
			"#{map.killTime,mode=IN,jdbcType=TIMESTAMP},",
			"#{map.result,mode=OUT,jdbcType=INTEGER});"
	})
	@Results({
			@Result(column="result", property="result", jdbcType= JdbcType.INTEGER)
	})
	@Options(statementType = StatementType.CALLABLE)
	void killByProcedure(@Param("map") Map map);
}
